﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Services;
using System.Web.Script.Serialization;

public partial class adm_vlg_gig : System.Web.UI.Page
{
    CommonPage cm3 = new CommonPage();
    VLGDataContext _db8 = new VLGDataContext();
    //string cateId = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;
        LoadListCategory();
        fillDulieuLenGrid("");

    }
    //protected void DataPager1_PreRender(object sender, EventArgs e)
    //{
    //    fillDulieuLenGrid("");

    //}
    protected void drp_cat_SelectedIndexChanged(object sender, EventArgs e)
    {
        string cateId = drp_cat.SelectedValue;
        fillDulieuLenGrid(cateId);
        // Session["catid"] = drp_cat.SelectedValue;
    }
    void fillDulieuLenGrid(string cateId)
    {
        IQueryable<Gig> gig = null;
        if (cateId != "-1" && cateId != "")
        {
            gig = from gg2 in _db8.Gigs
                  where gg2.idCategory == Convert.ToInt32(cateId)
                  orderby gg2.NgayTao descending
                  select gg2;
        }
        else
        {
            gig = from gg2 in _db8.Gigs
                  orderby gg2.NgayTao descending
                  select gg2;
        }

        GridView1.DataSource = gig;
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        fillDulieuLenGrid(drp_cat.SelectedValue);
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
    //Cập nhật ẩn hiện trên lưới
    public void chkAnHien_OnCheckedChanged(object sender, EventArgs e)
    {
        CheckBox chkAnHien = (CheckBox)sender;
        GridViewRow row = (GridViewRow)chkAnHien.NamingContainer;
        string cid = chkAnHien.ToolTip;
        bool status = chkAnHien.Checked;
        Category cate = _db8.Categories.SingleOrDefault(x => x.idCategory == int.Parse(cid));
        cate.AnHien = status;
        _db8.SubmitChanges();
        // fillDulieuLenGrid();
    }
    protected void linkAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect(ResolveClientUrl("~/qlcongviec.aspx?op=add"));
    }
    //protected void linkDelG_Click(object sender, EventArgs e)
    //{
    //    try
    //    {
    //        string lDel = ((LinkButton)sender).CommandArgument.ToString();
    //        Category giDel = _db8.Categories.Single(x => x.idCategory == int.Parse(lDel));
    //        _db8.Categories.DeleteOnSubmit(giDel);
    //        _db8.SubmitChanges();
    //        this.fillDulieuLenGrid();
    //    }
    //    catch
    //    {
    //        //this.fillDulieuLenGrid();
    //        //Literal1.Text = "<script type=\"text/javascript\">" +
    //        //                    "$(function() { " +
    //        //                    "alert(\"Không thể xóa danh mục đang sử dụng.\");" +
    //        //                    "});" +
    //        //                    "</script>";
    //    }
    //}
    protected void linkEditG_Click(object sender, EventArgs e)
    {

    }
    void LoadListCategory()
    {

        var category = (from c in _db8.Categories
                        select c).ToList();

        category.Insert(0, new Category { idCategory = -1, TenCate = "--Chọn danh mục bài viết--" });
        drp_cat.DataSource = category;
        drp_cat.DataTextField = "TenCate";
        drp_cat.DataValueField = "idCategory";
        drp_cat.DataBind();

        try
        {
            drp_cat.Items.FindByValue(Session["catid"].ToString()).Selected = true;
        }
        catch { }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="idAcc"></param>
    /// <returns>
    /// 0 : có lỗi
    /// 1 : xóa thành công
    /// </returns>
    /// Van chua kiem tra admin moi duoc thao tac
    [WebMethod]
    public static int AdminDeleteGig(string idGig)
    {

        if (!string.IsNullOrEmpty(idGig))
        {
            try
            {
                VLGDataContext _dataContext = new VLGDataContext();
                CommonPage _comment = new CommonPage();
                var idGigInt = Convert.ToInt32(idGig);
                var gig = _dataContext.Gigs.Select(p => p).Where(p => p.idGig == idGigInt).FirstOrDefault();
                var comment = _dataContext.GigComments.Where(c => c.idGig == idGigInt);
                var order = _dataContext.Orders.Where(o => o.idGig == idGigInt);
                foreach (var item in order)
                {
                    var gigState = _dataContext.GigStates.Where(s => s.idDH == item.idDH);
                    _dataContext.GigStates.DeleteAllOnSubmit(gigState);

                }
                _dataContext.Orders.DeleteAllOnSubmit(order);
                _dataContext.GigComments.DeleteAllOnSubmit(comment);
                _dataContext.Gigs.DeleteOnSubmit(gig);
                _dataContext.SubmitChanges();
                return 1;

            }
            catch
            {

                return 0;
            }

        }
        else return 0;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="idAcc"></param>
    /// <returns>
    /// 0 : có lỗi
    /// 1 : xóa thành công
    /// </returns>
    /// Van chua kiem tra admin moi duoc thao tac
    [WebMethod]
    public static GigJson AdminGetGig(string idGig)
    {

        if (!string.IsNullOrEmpty(idGig))
        {
            try
            {
                VLGDataContext _dataContext = new VLGDataContext();
                CommonPage _comment = new CommonPage();
                var idGigInt = Convert.ToInt32(idGig);
                //var gig = _dataContext.Gigs.Select(p => p).Where(p => p.idGig == idGigInt).FirstOrDefault();
                var gig = (from g in _dataContext.Gigs
                           where g.idGig == Convert.ToInt32(idGig)
                           select new GigJson
                           {
                               TenCongViec = g.TenCongViec,
                               MoTaCV = g.TuGioiThieu,
                               ipAddress = g.VLGAccount.ipAddress,
                               TenQuocGia = g.VLGAccount.TenQuocGia,
                               SoNgay = g.SoNgayCanDeXong.Value,
                               SoDonHang = g.Orders.Where(c => c.CustomerConfirm == true).Count(),
                               Video = GetVideo(g.LinkVideo,g.UrlHinh1)
                           }).SingleOrDefault();

                return gig;
            }
            catch
            {
                return null;
            }

        }
        else return null;
    }
    public static string GetVideo(string video, string image)
    {
        if (string.IsNullOrEmpty(video))
            //chưa xu ly de dong bo server và test : chưa lay dc duong dan cua request tai
            return "<img width=\"270px\" height=\"210px\" src=\"http://vieclamgap.net/upload/" + image + "\"  class=\"ava\" />";
        else
        {
            string[] tenfile = video.Split('.');
            string tenfilekoduoi = tenfile[0];
            return "<script type=\"text/javascript\">jwplayer(\"myplayer\").setup({skin:\"http://vieclamgap.net/adm_vlg/js/mediaplayer-510/glow.zip\",stretching:\"fill\",flashplayer: \"http://ViecLamGap.net/adm_vlg/js/mediaplayer-510/player.swf\",image: \"http://ViecLamGap.net/videos/hinh.jpg\",width: 270,height: 210,levels: [{ file: \"http://ViecLamGap.net/videos/" + video + "\" },{ file: \"http://ViecLamGap.net/videos/" + tenfilekoduoi + ".webm\" }]});</script>";
        }


    }
}

public class GigJson
{
    public GigJson()
    {

    }
    public string TenCongViec { get; set; }
    public string MoTaCV { get; set; }
    public string ipAddress { get; set; }
    public string TenQuocGia { get; set; }
    public int SoNgay { get; set; }
    public int SoDonHang { get; set; }
    public string Video { get; set; }

}